package io.renren.modules.company.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.JsonUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.company.dto.PersonBody;
import io.renren.modules.company.dto.PersonDTO;
import io.renren.modules.company.excel.PersonExcel;
import io.renren.modules.company.service.PersonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;
import java.util.Map;


/**
 * 员工
 *
 * @author tree
 * @since 1.0.0 2024-12-24
 */
@RestController
@RequestMapping("company/person")
@Tag(name = "员工")
public class PersonController {
    private static final Logger log = LoggerFactory.getLogger(PersonController.class);
    @Autowired
    private PersonService personService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String")
    })
    @RequiresPermissions("company:person:page")
    public Result<PageData<PersonDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<PersonDTO> page = personService.page(params);

        return new Result<PageData<PersonDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("company:person:info")
    public Result<PersonDTO> get(@PathVariable("id") Long id) {
        PersonDTO data = personService.get(id);

        return new Result<PersonDTO>().ok(data);
    }

    @PostMapping("qeuryPersonByCompany")
    @Operation(summary = "信息")
    @RequiresPermissions("company:person:info")
    public Result<List<PersonDTO>> qeuryPersonByCompany(@RequestBody String body) {
        log.info("qeuryPersonByCompany:" + body);
        PersonBody personBody = JsonUtils.parseObject(body, PersonBody.class);
        List<PersonDTO> data = personService.getByCompany(personBody.getCompanyname());

        return new Result<List<PersonDTO>>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("company:person:save")
    public Result save(@RequestBody PersonDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        personService.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("company:person:update")
    public Result update(@RequestBody PersonDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        personService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("company:person:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        personService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("company:person:export")
    public void export(@Parameter(hidden = true) @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<PersonDTO> list = personService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "员工", list, PersonExcel.class);
    }

}
